package com.example.figurehecheng.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.example.figurehecheng.common.constant.AutoFillConstant;
import com.example.figurehecheng.common.constant.MessageConstant;
import com.example.figurehecheng.common.context.BaseContext;
import com.example.figurehecheng.common.exception.AccountExist;
import com.example.figurehecheng.common.result.PageResult;
import com.example.figurehecheng.common.utils.AliOssUtil;
import com.example.figurehecheng.domain.dto.TourtableDTO;
import com.example.figurehecheng.domain.entity.Coupon;
import com.example.figurehecheng.domain.entity.Image;
import com.example.figurehecheng.domain.entity.Merchant;
import com.example.figurehecheng.domain.entity.Tourtable;
import com.example.figurehecheng.domain.vo.ImageVO;
import com.example.figurehecheng.domain.vo.TourtableVO;
import com.example.figurehecheng.mapper.TourtableMapper;
import com.example.figurehecheng.service.IMerchantService;
import com.example.figurehecheng.service.ITourtableService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 宇科工作室
 * @since 2024-10-18
 */
@Service
public class TourtableServiceImpl extends ServiceImpl<TourtableMapper, Tourtable> implements ITourtableService {


    @Autowired
    private IMerchantService iMerchantService;




    /**
     * 发布景点
     *
     * @param tourtable
     */
    @Override
    public void saveTour(TourtableDTO tourtable) {
        if (!ObjectUtil.isAllNotEmpty(tourtable.getName(), tourtable.getFeature(), tourtable.getHistoryBackground())) {
            throw new AccountExist(MessageConstant.COUPON_NOT_NULL);
        }
        Tourtable tour = BeanUtil.copyProperties(tourtable, Tourtable.class);
        tour.setShopId(BaseContext.getCurrentId().intValue());
        QueryWrapper<Merchant> merchantQueryWrapper = new QueryWrapper<Merchant>()
                .eq("id", BaseContext.getCurrentId());
        Merchant merchant = iMerchantService.getOne(merchantQueryWrapper);
        tour.setType(merchant.getRole());
        save(tour);
    }

    @Override
    public TourtableVO selectone() {
        TourtableVO tourtableVO = new TourtableVO();
        Long currentId = BaseContext.getCurrentId();
        List<TourtableVO> tourlist = new ArrayList<>();
        List<Tourtable> tour = lambdaQuery().
                eq(Tourtable::getShopId, currentId)
                .list();
        tour.forEach(item -> {
            try {
                tourlist.add(BeanUtil.copyProperties(item, TourtableVO.class));
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        if (tourlist.size() > 1) {
            System.out.println("发布项目过多");
        } else if (tourlist.size() == 1) {
            tourtableVO = tourlist.get(0);
        }
        return tourtableVO;
    }

    @Override
    public void updatetour(TourtableDTO tourtableDTO) {
        if (!ObjectUtil.isAllNotEmpty(tourtableDTO.getName(), tourtableDTO.getFeature(), tourtableDTO.getHistoryBackground())) {
            throw new AccountExist(MessageConstant.COUPON_NOT_NULL);
        }

        Tourtable tourtable = BeanUtil.copyProperties(tourtableDTO, Tourtable.class);
        System.out.println(tourtable);
        Tourtable tourtables = lambdaQuery()
                .eq(Tourtable::getShopId, BaseContext.getCurrentId())
                .one();

        if (!(tourtables==null)) {
            boolean update = lambdaUpdate()
                    .set(Tourtable::getName, tourtable.getName())
                    .set(Tourtable::getAddress, tourtable.getAddress())
                    .set(Tourtable::getDiscount, tourtable.getDiscount())
                    .set(Tourtable::getEndTime,tourtable.getEndTime())
                    .set(Tourtable::getOpenTime,tourtable.getOpenTime())
                    .set(Tourtable::getNumber,tourtable.getNumber())
                    .set(Tourtable::getFeature,tourtable.getFeature())
                    .set(Tourtable::getPrice,tourtable.getPrice())
                    .set(Tourtable::getTourType,tourtable.getTourType())
                    .set(Tourtable::getHistoryBackground,tourtable.getHistoryBackground())
                    .eq(Tourtable::getShopId, BaseContext.getCurrentId())
                    .update();
        }
//
        if (tourtables==null) {
            tourtable.setShopId(BaseContext.getCurrentId().intValue());
            Merchant merchant = iMerchantService.getOne(new QueryWrapper<Merchant>()
                    .eq("id", BaseContext.getCurrentId().intValue()));
            tourtable.setType(merchant.getRole());
            save(tourtable);
        }
    }

    @Override
    public void addvoideo(String urlname) {

        boolean update = lambdaUpdate()
                .set(Tourtable::getVideo, urlname)
                .eq(Tourtable::getShopId, BaseContext.getCurrentId())
                .update();
    }

    @Override
    public String sevoideo() {

        Long currentId = BaseContext.getCurrentId();

        Tourtable tour = lambdaQuery().
                eq(Tourtable::getShopId, currentId)
                .one();
        String video = tour.getVideo();


        return video;
    }


}



